<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_chart"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      initRankChart: null,
      allData: [],
      totalPage: '',
      curetPage: 1,
      timerId: '',
      startValue: 0,
      endValue: 9,
    }
  },
  created() {
    this.$socket.registerCallBack('rankData', this.getData)
  },
  computed: {
    ...mapState(['theme']),
  },
  watch: {
    theme() {
      this.initRankChart.dispose()
      this.initallData()
      this.screenAdapter()
      this.upDataCharts()
    },
  },
  mounted() {
    this.initallData()
    this.$socket.send({
      action: 'getData',
      socketType: 'rankData',
      chartName: 'rank',
      value: '',
    })

    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
    // this.getallData()
  },
  destroyed() {
    clearInterval(this.timerId)

    window.removeEventListener('resize', this.screenAdapter)
    this.$socket.unRegisterCallBack('rankData')
  },
  methods: {
    initallData() {
      console.log(this.theme)
      this.initRankChart = this.$chart.init(this.$refs.rank_chart, this.theme)
      const colorArr = [
        ['#0BA82C', '#4FF778'],
        ['#2E72BF', '#23E5E5'],
        ['#5052EE', '#AB6EE5'],
      ]
      let options = {
        title: {
          text: '▎地区销售排行',
          left: 20,
          top: 20,
        },
        grid: {
          top: '30%',
          left: '3%',
          right: '6%',
          bottom: '3%',
          containLabel: true,
        },
        tooltip: {
          // 当鼠标移入axis(坐标轴)时展示 底层的背景色
          trigger: 'axis',
          axisPointer: {
            // 展示的类型是线条类型
            type: 'shadow',
            shadowStyle: {
              shadowColor: 'rgba(0, 0, 0, 0.5)',
              shadowBlur: 10,
            },
            // 相等于 z-index 将层级调低
            z: 0,
          },
        },
        xAxis: {
          type: 'category',
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            type: 'bar',
            label: {
              show: true,
              position: 'top',
              textStyle: {
                color: 'white',
              },
            },

            itemStyle: {
              // barBorderRadius: [33, 33, 0, 0],

              color: arg => {
                let targetColorArr = null

                if (arg.value > 300) {
                  targetColorArr = colorArr[0]
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1]
                } else {
                  targetColorArr = colorArr[2]
                }

                return new this.$chart.graphic.LinearGradient(0, 0, 0, 1, [
                  // 0%
                  { offset: 0, color: targetColorArr[0] },
                  // 100%
                  { offset: 1, color: targetColorArr[1] },
                ])
              },
            },
          },
        ],
      }

      this.initRankChart.setOption(options)
      this.initRankChart.on('mouseover', () => {
        this.timerId && clearInterval(this.timerId)
      })
      // 鼠标移出图表的时候 定时器开启
      this.initRankChart.on('mouseout', () => {
        this.setIntervalData()
      })
    },
    upDataCharts() {
      //   let start = (this.curetPage - 1) * 10
      //   let end = this.curetPage + 10
      //   console.log(start, end)
      //   let showData = this.allData.splice(start, end)
      let sellerNames = this.allData.map(item => item.name)
      let sellerValues = this.allData.map(item => item.value)
      const dataOption = {
        xAxis: {
          data: sellerNames,
        },
        dataZoom: {
          // 区域缩放组件
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
        series: [
          {
            data: sellerValues,
          },
        ],
      }
      this.initRankChart.setOption(dataOption)
    },
    async getData(ret) {
      // let res = await this.$axios.get('rank.json')
      // this.allData = res.data
      this.allData = JSON.parse(ret)
      this.allData.sort((a, b) => {
        return b.value - a.value
      })
      this.upDataCharts()
      this.setIntervalData()
    },
    setIntervalData() {
      this.timerId = setInterval(() => {
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0
          this.endValue = 9
        } else {
          this.endValue++
          this.startValue++
        }
        this.upDataCharts()
      }, 3000)
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.rank_chart.offsetWidth / 100) * 3.6
      // 和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize / 1.5,
          },
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize,
            },
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
            },
          },
        ],
      }
      this.initRankChart.setOption(adapterOption)
      // 手动的调用图表对象的resize 才能产生效果
      this.initRankChart.resize()
    },
  },
}
</script>

<style></style>
